package com.ocom.webapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.MapKey;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.feign.PayConfigClientService;
import com.ocom.webapi.service.ConsumeAccountService;
import com.ocom.webapi.vo.accounttypepage.DetailsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service
public class ConsumeAccountServiceImpl implements ConsumeAccountService {

    @Autowired
    private PayConfigClientService payConfigClientService;

    @Autowired
    private HumanClientService humanClientService;
    /**
     * 获取账户信息列表（ConsumeAccount/details）
     * */
    @Override
    public Result ConsumeAccountDetails(DetailsVo detailsVo) {
        String deptIds = "";
        if(detailsVo.getDeptId()!=0l){
            deptIds = detailsVo.getDeptId().toString();
            Result result = humanClientService.getDeptList(detailsVo.getComId(),detailsVo.getDeptId(),0);
            if (result.getCode()!=200){
                return ResultUtil.error(result.getCode());
            }
            try {
                List<EntityDeptInfo> entityDeptInfos = JSON.parseArray(JSON.toJSONString(result.getData()),EntityDeptInfo.class);
                List<Long> depIds= entityDeptInfos.stream().map(EntityDeptInfo -> EntityDeptInfo.getDeptId()).collect(Collectors.toList());
                for (Long dep: depIds){
                    deptIds+=","+dep;
                }
            }catch (Exception e){
                log.error("异常" +e);
            }
        }

        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_comId(),detailsVo.getComId());
        map.put(mapKey.getKey_deptIds(),deptIds);
        map.put(mapKey.getKey_pCode(),detailsVo.getPCode());
        map.put(mapKey.getKey_pName(),detailsVo.getPName());
        map.put(mapKey.getKey_cardNo(),detailsVo.getCardNo());
        map.put(mapKey.getKey_accountNo(),detailsVo.getAccountNo());
        map.put(mapKey.getKey_accTypeId(),detailsVo.getAccTypeId());
        map.put(mapKey.getKey_page(),detailsVo.getPage());
        map.put(mapKey.getKey_size(),detailsVo.getSize());
        map.put("subAccount",detailsVo.getSubAccount());
        return payConfigClientService.GetListRecharge(map);
    }

    /**
     * 个人账户充值(ConsumeAccount/personalRecharge)
     * */
    @Override
    public Result ConsumeAccountRecharge(Map<String,Object> map) {
        map.put("userId",SecurityUtils.getYoCiUser().getId());
        map.put("userName",SecurityUtils.getUsername());
        map.put("merchId",SecurityUtils.getYoCiUser().getMerchId());
        Object rechargeType = map.get("rechargeType");
        if (null != rechargeType && "1".equals(String.valueOf(rechargeType))) {
//            YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//            if (yoCiUser.getRole() == 41L || yoCiUser.getRole() == 42L || yoCiUser.getMerchId()>0l) {
//               return ResultUtil.error(ResultEnum.Err_2044);
//            }
            return payConfigClientService.updateSubsidy(map);
        }
        return payConfigClientService.UpdateListRecharge(map);
    }

    /**
     * 个人账户发卡（ConsumeAccount/issuing）
     * */
    @Override
    public Result ConsumeAccountIssuing(Map<String, Object> map) {
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_user(), SecurityUtils.getUsername());

        /**
         * cardNo: "77988"
         * comId: 1
         * endDate: "2022-12-22"
         * pId: 2519
         */

        Result result = payConfigClientService.AddCardInfo(map);

        if(result.getCode()!=200){
            return result;
        }
        if(map.containsKey("charge")){
            Object chargeObject = map.get("charge");
            if(chargeObject!=null){
                Integer charge = (Integer)chargeObject;
                if(charge>0){
                    //充值
                    Map<String,Object> mapCharge=new HashMap<>();
                    mapCharge.put("comId",map.get("comId"));
                    mapCharge.put("pId",map.get("pId"));
                    mapCharge.put("rechargeMoney",charge);

                    mapCharge.put("userId",SecurityUtils.getYoCiUser().getId());
                    mapCharge.put("userName",SecurityUtils.getUsername());

                    payConfigClientService.UpdateListRecharge(mapCharge);
                }
            }
        }
        return result;
    }

    /**
     * 个人账户挂失/解挂（ConsumeAccount/cardLoss）
     * */
    @Override
    public Result ConsumeAccountCardLoss(Map<String, Object> map) {
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_user(), SecurityUtils.getUsername());
        return payConfigClientService.UpdatecardState(map);
    }
}
